package com.k.kc.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;
import java.util.Random;

/**
 * @author k
 */
public class ByteUtils {
    private static final char[] HEX_DIGITS = {
            '0' , '1' , '2' , '3' ,
            '4' , '5' , '6' , '7' ,
            '8' , '9' , 'a' , 'b' ,
            'c' , 'd' , 'e' , 'f'
    };

    // ************************************************************************************************************
    // Others to byte[]
    // ************************************************************************************************************

    public static byte[] fromInt(int i){
        byte[] bytes = new byte[4];
        bytes[0] = (byte) ((i>>24) & 0xff);
        bytes[1] = (byte) ((i>>16) & 0xff);
        bytes[2] = (byte) ((i>>8) & 0xff);
        bytes[2] = (byte) (i & 0xff);
        return bytes;
    }
    public static byte[] fromStr(String s){
        return s.getBytes();
    }

    public static byte[] fromStr(String s,String charsetName) throws UnsupportedEncodingException {
        return s.getBytes(charsetName);
    }

    public static byte[] fromFile(File f){
        return new byte[(int) f.length()];
    }
    public static byte[] fromBase64(String base64Str,Charset charset){
        return Base64.getDecoder().decode(base64Str.getBytes(charset));
    }


    // ************************************************************************************************************
    // byte[] to Others
    // ************************************************************************************************************

    public static String toStr(byte[] b){
        return new String(b);
    }

    public static String toStr(byte[] b,String charsetName) throws UnsupportedEncodingException {
        return new String(b,0,b.length,charsetName);
    }

    private static String toHex0(byte[] bytes) {
        StringBuilder ret = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            ret.append(HEX_DIGITS[(b >> 4) & 0x0f]);
            ret.append(HEX_DIGITS[b & 0x0f]);
        }
        return ret.toString();
    }
    public static String toHex(byte[] b) {
        char[] chs = new char[b.length * 2];
        for(int i = 0, offset = 0; i < b.length; i++) {
            chs[offset++] = HEX_DIGITS[b[i] >> 4 & 0xf];
            chs[offset++] = HEX_DIGITS[b[i] & 0xf];
        }
        return new String(chs);
    }

    public static String toBase64(byte[] b){
        return Base64.getEncoder().encodeToString(b);
    }

    public static int toInt(byte[] b){
        int length = b.length;
        int i = 0;
        if(length == 4){
            return b[i] & 0xff | ((b[i+1] & 0xff )<< 8) | (b[i+2] & 0xff)<<16 | (b[i+3] & 0xff)<<24;
        } else if(length == 3){
            return b[i] & 0xff | ((b[i+1] & 0xff )<< 8) | (b[i+2] & 0xff)<<16;
        } else if(length == 2){
            return b[i] & 0xff | ((b[i+1] & 0xff )<< 8);
        } else if(length == 1){
            return b[i] & 0xff;
        } else {
            throw new IllegalArgumentException("Byte length valid");
        }
    }

    public static void main(String[] args) {
        byte[] key256Byte = "12345678901234567890123456789012".getBytes(Charset.forName("utf-8"));
        System.out.println(key256Byte.length);
        String key256Base64 = toBase64(key256Byte);
        System.out.println("key256Base64:"+key256Base64);


        byte[] zb = fromBase64(key256Base64, StandardCharsets.UTF_8);
        String zs = new String(zb,Charset.forName("utf-8"));
        System.out.println(zb.length);
        System.out.println("zs:"+zs);

        int a =123;
        byte[] b= fromInt(a);
        System.out.println("a->byte:"+b.length);

        Random r = new Random();
        byte[] bytes = new byte[32];
        r.nextBytes(bytes);
        for(byte db:bytes){
            System.out.println(db);
        }

    }
}
